package com.myapp.wicket;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.wicket.PageParameters;
import org.apache.wicket.extensions.markup.html.repeater.data.table.DefaultDataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.extensions.model.AbstractCheckBoxModel;
import org.apache.wicket.markup.html.form.Form;

import dto.ExtendedOrder;
import repository.OrderRepository;

public final class WatchConfirmedOrdersPage extends WebPage implements AuthenticatedWebPage {

    private Set<ExtendedOrder> selected=new HashSet<ExtendedOrder>();    
    private OrderRepository orderRepository = new OrderRepository();
    private static List<ExtendedOrder> extendedOrders = null;
    
        public WatchConfirmedOrdersPage() {
        super();
        extendedOrders=orderRepository.GetOrdersForConfirm();
        add(new FeedbackPanel("feedback"));

        //generating columns
        List<IColumn<ExtendedOrder>> columns = new ArrayList<IColumn<ExtendedOrder>>();
        columns.add(new CheckBoxColumn<ExtendedOrder>() 
        {
            @Override
            protected IModel<Boolean> newCheckBoxModel(final IModel<ExtendedOrder> rowModel) 
            {
                return new AbstractCheckBoxModel() 
                    {
                        @Override
                        public void unselect() {
                            selected.remove(rowModel.getObject());
                        }
                        
                        @Override
                        public void select() 
                        {
                            selected.add(rowModel.getObject());                            
                        }
			@Override
			public boolean isSelected() {
                            return selected.contains(rowModel.getObject());
			}
			@Override
                        public void detach() {
                            rowModel.detach();
                        }
                    };
            }
        });
        columns.add(new PropertyColumn<ExtendedOrder>(Model.of("Id"), "id"));
        columns.add(new PropertyColumn<ExtendedOrder>(Model.of("Order Name"), "orderName"));
	columns.add(new PropertyColumn<ExtendedOrder>(Model.of("Total Price"), "totalPrice","totalPrice"));
	columns.add(new PropertyColumn<ExtendedOrder>(Model.of("User Name"), "userName"));
        columns.add(new PropertyColumn<ExtendedOrder>(Model.of("User Surname"), "surname"));
        
        Form<?> form = new Form<Void>("conform") {
			@Override
			protected void onSubmit() 
                        {
                            String ids="";
				for (ExtendedOrder extendedOrder : selected) {
                                     ids+=extendedOrder.id+",";   
                                }
                                if (ids.length()>0)
                                {
                                    ids=ids.substring(0,ids.length()-1);
                                    orderRepository.UpdateSatusToConfirmedByAdmin(ids);
                                    setResponsePage(Index.class);  
                                }
			}
         };
        add(form);
        form.add(new DefaultDataTable<ExtendedOrder>("extendedOrders", columns, new OrderProvider(extendedOrders), 10));
    }
    
        public WatchConfirmedOrdersPage(PageParameters params) {
        super();
        extendedOrders=orderRepository.GetOrdersForConfirm();
        add(new FeedbackPanel("feedback"));
        //generating columns
        List<IColumn<ExtendedOrder>> columns = new ArrayList<IColumn<ExtendedOrder>>();
        columns.add(new CheckBoxColumn<ExtendedOrder>() 
        {
            @Override
            protected IModel<Boolean> newCheckBoxModel(final IModel<ExtendedOrder> rowModel) 
            {
                return new AbstractCheckBoxModel() 
                    {
                        @Override
                        public void unselect() {
                            selected.remove(rowModel.getObject());
                        }
                        
                        @Override
                        public void select() 
                        {
                            selected.add(rowModel.getObject());                            
                        }
			@Override
			public boolean isSelected() {
                            return selected.contains(rowModel.getObject());
			}
			@Override
                        public void detach() {
                            rowModel.detach();
                        }
                    };
            }
        });
        columns.add(new PropertyColumn<ExtendedOrder>(Model.of("Id"), "id"));
        columns.add(new PropertyColumn<ExtendedOrder>(Model.of("Order Name"), "orderName", "orderName"));
	columns.add(new PropertyColumn<ExtendedOrder>(Model.of("Total Price"), "totalPrice","totalPrice"));
	columns.add(new PropertyColumn<ExtendedOrder>(Model.of("User Name"), "userName"));
        columns.add(new PropertyColumn<ExtendedOrder>(Model.of("User Surname"), "surname"));
        
        Form<?> form = new Form<Void>("conform") {
			@Override
			protected void onSubmit() 
                        {
                            String ids="";
				for (ExtendedOrder extendedOrder : selected) {
                                     ids+=extendedOrder.id+",";   
                                }
                                if (ids.length()>0)
                                {
                                    ids=ids.substring(0,ids.length()-1);
                                    orderRepository.UpdateSatusToConfirmedByAdmin(ids);
                                    setResponsePage(Index.class);  
                                }
			}
         };
        add(form);
        form.add(new DefaultDataTable<ExtendedOrder>("extendedOrders", columns, new OrderProvider(extendedOrders), 10));
    }
}
